package com.xc.erp.modules.erp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.common.utils.StringUtils;
import com.xc.erp.modules.erp.domain.pojo.JshSupplier;
import com.xc.erp.modules.erp.domain.pojo.JshTenant;
import com.xc.erp.modules.erp.mapper.JshTenantMapper;
import com.xc.erp.modules.erp.service.JshTenantService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
*
*/
@Service
public class JshTenantServiceImpl extends ServiceImpl<JshTenantMapper, JshTenant>
implements JshTenantService {

    @Override
    public Page<JshTenant> list(Integer pageSize, Integer pageNum, String loginName) {
        Page<JshTenant> jshTenantPage = new Page<>(pageSize, pageNum);
        QueryWrapper<JshTenant> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(loginName)){
            queryWrapper.like("login_name",loginName);
        }
        Page<JshTenant> page = baseMapper.selectPage(jshTenantPage, queryWrapper);
        return page;
    }

    @Override
    public boolean checkIsNameExist(Long id,String loginName) {
        QueryWrapper<JshTenant> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        JshTenant jshTenant = baseMapper.selectOne(queryWrapper);

        if (jshTenant!=null){
            if (id!=null){
                if (id.equals(jshTenant.getId())){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void batchSetStatus(boolean status, Long[] ids) {
        List<JshTenant> tenantList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            JshTenant tenant = new JshTenant();
            tenant.setEnabled(status);
            tenant.setId(ids[i]);
            tenantList.add(tenant);
        }
        this.updateBatchById(tenantList);
    }
}
